import axios from 'axios'
import store from '../store'
import router from '../router'
import baseURL from './url'
import qs from 'qs'
import Vue from 'vue'
const ajaxfile = axios.create({
  baseURL: baseURL,
  timeout: 30000
})
ajaxfile.interceptors.request.use(config => {
  // 设置请求头
  config.headers = {
    'Content-Type': 'multipart/form-data', // 模拟form表单方式提交请求
    token: store.getters['session/token'] === undefined ? '' : store.getters['session/token'] // 设置token
  }
  return config
}, error => {
  return Promise.reject(error)
})
const vm = new Vue({})
ajaxfile.interceptors.response.use((response) => {
  console.log('respon', response)
  // 拦截响应，做统一处理
  if (response.data.code) {
    switch (response.data.code) {
      case 201:
      case 401:
        store.state.isLogin = false
        router.push({
          path: '/login',
          query: {
            redirect: router.currentRoute.fullPath
          }
        })
    }
  }
  return response
}, (error) => {
  console.log('error', error)
  setTimeout(() => {
    vm.$Message.destroy()
  }, 1000)
  return Promise.reject(error)
})
export default ajaxfile
